<?php
/**
 *	@func memcached 缓存 百度云空间Memcached
 *	@author midoks
 *	@link midoks.cachecha.com
 */
class BaeMcCache{

	public $cfg = array(
		'host' => '127.0.0.1',
		'port' => 11211,
		'timeout'=> 30,
		'time'	=> '',		//保存时间特殊设置
	);						//$cfg配置
	private $link = false;	//连接的资源

	public function __construct(){
		if(file_exists('BaeMemcache.class.php')){
			require_once('BaeMemcache.class.php');
			$this->link = new BaeMemcache();
		}else{
			$this->link = false;
		}
		//$this->connect();
	}

	//@func 连接memcache服务器
	public function connect(){
		$this->link->connect($this->cfg['host'], 
							$this->cfg['port'],
							$this->cfg['timeout']);
	}

	/**
	 *	@func 向memcache服务器中,插入值
	 *	@param $key key值
	 *	@param $value value值
	 *	@return 返回
	 */
	public function write($key, $value){
		if($this->link){
			if(!empty($this->cfg['time'])){
				$bool = $this->link->set($key, $value, $this->cfg['time']);
			}	
			$bool = $this->link->set($key, $value, $this->cfg['timeout']);
			//var_dump($bool);
			return $bool;
		}
	}

	/**
	 * @func 向memcache服务器中,获取值
	 * @param $key key值
	 * @ret array
	 */
	public function read($key){
		if($this->link){
			//var_dump($key);
			$data = $this->link->get($key);
			//var_dump($data);
			//$data = json_decode($data, true);
			//var_dump($data);
			if(!is_array($data)){
				return false;
			}
			return $data;
		}
	}

	/**
	 *	@func 在memcache服务器中删除一个元素
	 *	@param string $key 要删除的key值
	 */
	public function delete($key){
		if($this->link){
			return $this->link->delete($key);
		}	
	}

	//@func 清空memcache中所有数据
	public function flush(){
		if($this->link){
			return $this->link->flush();
		}	
	}

	//清空过期数据
	public function flush_expire(){}
}
?>
